﻿<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=true">
</script>

<script type="text/javascript">

	var map;
	var hcmCity = new google.maps.LatLng(10.7930593, 106.62913040000001);
	var marker;
	var geoCoder;
	
	var latTxb;
	var lngTxb;
	var addrTxt;

	function initialize() {
	
		//alert("alert");
	
		geoCoder = new google.maps.Geocoder();

		latTxb = document.getElementById("lattxb");
		lngTxb = document.getElementById("lngtxb");
		addrTxt = document.getElementById("addrtxt");
		
		var latlng = hcmCity;
		var myOptions = {
		  zoom: 8,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP  
		};
		
		map = new google.maps.Map(document.getElementById("map_canvas"),
			myOptions);
			
		marker = new google.maps.Marker({
			position: latlng, 
			map: map, 
			title:"Hello World!"
		});
			
		google.maps.event.addListener(map, 'click', function(me) {
				
			noteTarget(me.latLng);
			}
			);		
			
			
	}
	
	function noteTarget(latLng) {
	
		latTxb.value = latLng.lat();
		lngTxb.value = latLng.lng();
		
		marker.setPosition(latLng);
		
		if (latLng) {
			
			var geocoderReq = {
				location: latLng
			};
			
			geoCoder.geocode(geocoderReq, function(results, status) {
			
				if (status == google.maps.GeocoderStatus.OK) {
				
					addrTxt.value = results[0].formatted_address;
					
				}
					
			});
		}
	}
  
	function focusOn(latLng) {
	
		noteTarget(latLng);		
		map.panTo(latLng);	
	}
	
	function findTarget(addr) {
	
		if (addr) {
			
			var geocoderReq = {
				address: addr
			};
			
			geoCoder.geocode(geocoderReq, function(results, status) {
			
				if (status == google.maps.GeocoderStatus.OK) {
				
					focusOn(results[0].geometry.location);
					addrTxt.value = results[0].formatted_address;
					
					}
					
			});
		}
	} 

</script>